#!/bin/bash
# script name: 容器环境部署/卸载脚本
# author: subushi
# update date: 2023.12.13

echo "select"
echo -e "\033[1;42;37m 1.Install Network \033[0m"
echo "-------------------------------------------------------"
echo -e "\033[1;42;37m 2.Install Docker \033[0m"
echo "-------------------------------------------------------"
echo -e "\033[1;42;37m 3.Remove  Docker \033[0m"

read -p "Input selection 1/3：" choice

case $choice in

  1)
# 网卡变量
ens=(`ip a | grep 'ens' | awk -F ": " 'NR==1{print $2}'`)


# 网卡配置，在控制台执行，否则重启网络后脚本会被终止
echo "TYPE=Ethernet
BOOTPROTO=static
NAME=$ens
DEVICE=$ens
ONBOOT=yes
DEFROUTE=yes

PROXY_METHOD=none
BROWSER_ONLY=no
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy

IPADDR=192.168.10.1
NETMASK=255.255.255.0
GATEWAY=192.168.10.2
DNS1=8.8.8.8
DNS2=114.114.114.114" > /etc/sysconfig/network-scripts/ifcfg-$ens

systemctl restart network

ping -c 3 www.baidu.com


# 修改内核参数(虚拟机操作)
# sed -i 's/rhgb quiet LANG=zh_CN.UTF-8/vga=817 LANG=zh_CN.UTF-8/' /boot/grub2/grub.cfg


# 修改终端提示符样式
sed -i 's/W]/w]/' /etc/bashrc


# 关闭不必要的服务
systemctl stop firewalld
systemctl disable firewalld
systemctl stop postfix
systemctl disable postfix
systemctl stop NetworkManager
systemctl disable NetworkManager
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
  ;;


  2)
# 配置网络源
cd /etc/yum.repos.d/ && mkdir repo_backup && mv ./*.repo ./repo_backup

cat <<EOF | tee /etc/yum.repos.d/aliyun.repo
[base]
name=base
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
enabled=1
gpgcheck=0

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/
enabled=1
gpgcheck=0

[aliyun-os]
name=aliyun-os
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
enabled=1
gpgcheck=0

[aliyun-epel]
name=aliyun-epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=0

[aliyun-extra]
name=aliyun-extra
baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/
enabled=1
gpgcheck=0
EOF

yum clean all
yum makecache


# 补充常用包
yum   install   -y   ntsysv   bash-completion   bind-utils   net-tools   tree   vim   wget   telnet   zip   unzip


# 添加 docker-ce 源
yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo


# 开启核心转发
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p


# 安装 docker 程序包
yum  install  -y  docker-ce-24.0.2  docker-ce-cli-24.0.2  containerd.io  docker-compose


# 导入 docker 配置
mkdir -p /etc/docker

cat <<EOF | tee /etc/docker/daemon.json
{
  "registry-mirrors": ["https://bp857gis.mirror.aliyuncs.com"],
  "log-driver":"json-file",
  "log-opts": {"max-size":"500m", "max-file":"3"}
}
EOF


# 启动 docker 服务
systemctl daemon-reload
systemctl enable --now docker
  ;;


  3)
# 删除容器、镜像
docker stop $(docker ps -aq)
docker rm   $(docker ps -aq)
docker rmi  $(docker images -q)
systemctl stop docker
# ps -ef | grep docker | awk '{print $2}' | xargs kill -9


# 删除 docker 所在目录数据
rm -rf /etc/docker
rm -rf /run/docker
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
systemctl daemon-reload


# 卸载 docker 和 compose 相关包
yum list installed | grep '^docker' | awk '{print $1}'| xargs yum remove -y
yum list installed | grep '^docker-compose' | awk '{print $1}' | xargs yum remove -y
  ;;


  *)
  echo "Invalid selection"
  ;;
esac
